import BaseEvent from '@/components/events/BaseEvent'
import { isJSON, getCompIdByParam } from '@/plugins/utils'
import { nextTick } from 'vue'
import { EventBus } from '@/store/index'

export default class ShowOrHiddenEvent extends BaseEvent{
  constructor() {
    super();
  }

  excute({item, event}){
    this.item = item;
    this.event = event;

    let relateCompStr = event.comps[0].attrs.value;
    if(relateCompStr && relateCompStr.length > 0 && isJSON(relateCompStr)){
      let relateCompParam = JSON.parse(relateCompStr);
      let displayCompId = getCompIdByParam(relateCompParam.displayCompId) || relateCompParam.displayCompId;
      let hiddenCompId = getCompIdByParam(relateCompParam.hiddenCompId) || relateCompParam.hiddenCompId;
      //不在支持这种配置 可以替换为下面的配置更为灵活
      // let timeCompIdObj = relateCompParam.timeCompId;//{"timeCompId":{time:"",compId:""}}
      // if(timeCompIdObj){
      //   const end = new Date(timeCompIdObj.time).getTime();
      //   const now = new Date().getTime();
      //   const endTime = end - now;
      //   if(endTime < 0){
      //     displayCompId = timeCompIdObj.compId;
      //   }
      // }
      let time = relateCompParam.time;//{time:"2021/12/1"}} 到这个时间点触发事件 不到时间点直接返回
      if(time) {
        const end = new Date(time).getTime();
        const now = new Date().getTime();
        const endTime = end - now;
        if(endTime > 0){
          return
        }
      }

      // 站点2412&popId=3 当this.bindData中的数据大于20的时候隐藏数据
      let isInVisible = relateCompParam.isInVisible
      let key = relateCompParam.key
      if(isInVisible && key) {
        if(!this.bindData[key] || this.bindData[key] && this.bindData[key] <= isInVisible){
          return
        }
      }

      // 小于某个值的话就显示隐藏某两个组件 站点2485&popId = 4
      let isVisible = relateCompParam.isVisible
      if(isVisible && key) {
        if(this.bindData[key] < isVisible){
         [displayCompId,hiddenCompId] = [hiddenCompId, displayCompId]
        }
      }

      if(displayCompId) {
        if(item.isInGroupCarousel) {
          const displayCompData = this.componentMap[displayCompId];
          displayCompId = displayCompId + '_' + item.commonAttr.itemIndex + '_' + displayCompData.commonAttr.qIndex;
        }
        this.store.commit("addNeedRecoverCompId", this.componentMap[displayCompId]);
        this.store.commit("updateComponentAttr", {id: displayCompId, commonAttr: { isVisible: true } })
      }
      if(hiddenCompId) {
        if(item.isInGroupCarousel) {
          const hiddenCompData = this.componentMap[hiddenCompId];
          hiddenCompId = hiddenCompId + '_' + item.commonAttr.itemIndex + '_' + hiddenCompData.commonAttr.qIndex;
        }
        this.store.commit("addNeedRecoverCompId", this.componentMap[hiddenCompId]);
        this.store.commit("updateComponentAttr", {id: hiddenCompId, commonAttr: { isVisible: false } })
      }

      let hiddenComps = relateCompParam.hiddenCompIds && relateCompParam.hiddenCompIds.split(",");
      _.forEach(hiddenComps, (hiddenCompId) => {
        if(hiddenCompId){
          this.store.commit("addNeedRecoverCompId", this.componentMap[hiddenCompId]);
          this.store.commit("updateComponentAttr", {id: hiddenCompId, commonAttr: { isVisible: false } })
        }
      })
      if(item.isInGroupCarousel) {
        const currentPage = this.pages[this.pageIndex];
        if(currentPage.isAdjustPageHeight) {
          const groupCompId = item.groupCompId
          nextTick(() => {
            EventBus.$emit('adjustPageHeight', groupCompId) 
          })
        }
      }
    }
  }
}
